package com.cans.dto;

import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.List;

/**
 * @author: cans
 * @date: 2018/4/22
 * @description:
 */
public class PageRes<T> implements Serializable {

    /**
     * 状态字段 0:成功
     */
    private Integer code;
    /**
     * 错误消息
     */
    private String msg;
    /**
     * 总条数
     */
    private long count;
    /**
     * 数据
     */
    private List<T> data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    /**
     * 转换方法
     * @param page
     * @return
     */
    public PageRes<T> transfer(Page<T> page){
        this.code = 0;
        try {
            this.count = page.getTotalElements();
            this.data = page.getContent();
        } catch (Exception e) {
            this.code = 1;
            this.msg = e.getMessage();
        }
        return this;
    }
}
